//
//  ThreeDPublishCell.m
//  ShrimpCrowdordering
//
//  Created by jimmy on 2018/8/25.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "ThreeDPublishCell.h"
#import "ThreeDPublishCollectionViewCell.h"
#import "ThreeDSeasonCollectionViewCell.h"
#import "HorizontalPageFlowlayout.h"

#import "UIView+Utils.h"

@interface ThreeDPublishCell()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, assign) CGFloat  cellHeight;
@property (nonatomic, strong) NSIndexPath * indexpath;
@property (nonatomic, strong) UICollectionView * collectionView;
///传入的标题的字符串
@property (nonatomic,strong)  NSArray  * titleArray;
///传入的背景图片
@property (nonatomic,strong)  NSArray  * bjImageArray;
@property (nonatomic,assign)  NSInteger  itemCount;

@end

@implementation ThreeDPublishCell

+ (ThreeDPublishCell *)cellWithTableView:(UITableView *)tableView andIndexPath:(NSIndexPath *)indexPath andCellHeight:(CGFloat)height andTitleArray:(NSArray *)titleArray andBjImageArray:(NSArray *)bjImageArray lines:(NSInteger)lines itemCount:(NSInteger)itemCount andCellId:(NSString *)cellId andItemHeight:(NSInteger)itemHeight andTypeInt:(NSInteger)typeInt;
{
//    static NSString *cellID = @"ThreeDPublishCell";
    ThreeDPublishCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        cell = [[ThreeDPublishCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        cell.indexpath = indexPath;
        cell.cellHeight = height;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor whiteColor];
    }
    for (UIView *subView in cell.subviews) {
        [subView removeFromSuperview];
    }
    
    cell.typeInt = typeInt;
    cell.titleArray = titleArray;
    cell.bjImageArray = bjImageArray;
    cell.itemCount = itemCount;
    cell.selectIndex = 0;//如果是默认选中第0个就赋值：0
    [cell creatCollectionViewWithLine:lines itemCount:itemCount itemHeight:itemHeight];
    return cell;
}

- (void)setSelectIndex:(NSInteger)selectIndex
{
    _selectIndex = selectIndex;
    
//    [self.collectionView reloadData];
//    [self.collectionView.collectionViewLayout invalidateLayout];
}

-(void)creatCollectionViewWithLine:(NSInteger)lines itemCount:(NSInteger)itemCount itemHeight:(NSInteger)itemHeight
{
    HorizontalPageFlowlayout *layout = [[HorizontalPageFlowlayout alloc] initWithRowCount:lines itemCountPerRow:2];
    [layout setColumnSpacing:0 rowSpacing:0 edgeInsets:UIEdgeInsetsMake(0, ScreenX375(10), 0, 0)];
    layout.itemSize = CGSizeMake(SCREEN_WIDTH/2-ScreenX375(20),itemHeight);
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    //设置CollectionView的属性
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.cellHeight) collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.scrollsToTop = NO;
    self.collectionView.pagingEnabled = NO;
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    
    [self addSubview:self.collectionView];
    
    
    [self.collectionView registerClass:[ThreeDSeasonCollectionViewCell class] forCellWithReuseIdentifier:@"ThreeDSeasonCollectionViewCell"];
    
    
    [self.collectionView registerClass:[ThreeDPublishCollectionViewCell class] forCellWithReuseIdentifier:@"ThreeDPublishSexCollectionViewCell"];
    [self.collectionView registerClass:[ThreeDPublishCollectionViewCell class] forCellWithReuseIdentifier:@"ThreeDPublishAgeCollectionViewCell"];
    [self.collectionView registerClass:[ThreeDPublishCollectionViewCell class] forCellWithReuseIdentifier:@"ThreeDPublishSizeCollectionViewCell"];
    [self.collectionView registerClass:[ThreeDPublishCollectionViewCell class] forCellWithReuseIdentifier:@"ThreeDPublishTypeCollectionViewCell"];
    [self.collectionView registerClass:[ThreeDPublishCollectionViewCell class] forCellWithReuseIdentifier:@"ThreeDPublishAccessoryCollectionViewCell"];
    [self.collectionView registerClass:[ThreeDPublishCollectionViewCell class] forCellWithReuseIdentifier:@"ThreeDPublishChannelCollectionViewCell"];
    [self.collectionView registerClass:[ThreeDPublishCollectionViewCell class] forCellWithReuseIdentifier:@"ThreeDPublishBjCollectionViewCell"];
    
}

#pragma mark  设置CollectionView每组所包含的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.itemCount;
}

#pragma mark  设置CollectionCell的内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.typeInt == 2) {
        ThreeDSeasonCollectionViewCell *cell = [ThreeDSeasonCollectionViewCell cellWithCollectionView:collectionView andIndexPath:indexPath andTitleArray:self.titleArray andBjImageArray:self.bjImageArray];
        cell.tag = 2222;
        cell.titleLabel.text = self.titleArray[indexPath.row];
        cell.contentImageView.image = [UIImage imageNamed:self.bjImageArray[indexPath.row]] ;
        cell.contentImageView.contentMode = UIViewContentModeScaleToFill;
        if (self.selectIndex != indexPath.row) {
            cell.rightImage.image = [UIImage imageNamed:@"未选择"];
            cell.titleLabel.textColor = FONTCOLOR_BLACK;
        }else{
            cell.rightImage.image = [UIImage imageNamed:@"选择"];
            cell.titleLabel.textColor = UIColorFromRGB(@"#f21934", 1);
        }
        
        return cell;
    }else if (self.typeInt == 4){
        ThreeDPublishCollectionViewCell *cell = [ThreeDPublishCollectionViewCell cellWithCollectionView:collectionView andIndexPath:indexPath andTitleArray:self.titleArray andBjImageArray:self.bjImageArray andCellId:@"ThreeDPublishSizeCollectionViewCell"];
        cell.tag = 4444;
        cell.titleLabel.hidden = YES;
        cell.contentLabel.hidden = NO;
        cell.contentImageView.contentMode = UIViewContentModeScaleAspectFit;
        [cell.contentImageView sd_setImageWithURL:[NSURL URLWithString:self.bjImageArray[indexPath.row]]];
        cell.contentLabel.text = self.titleArray[indexPath.row];
        if (self.selectIndex != indexPath.row) {
            cell.rightImage.image = [UIImage imageNamed:@"未选择"];
            cell.contentLabel.textColor = FONTCOLOR_BLACK;
        }else{
            cell.rightImage.image = [UIImage imageNamed:@"选择"];
            cell.contentLabel.textColor = UIColorFromRGB(@"#f21934", 1);
        }
        return cell;
    }else if (self.typeInt == 0){
        ThreeDPublishCollectionViewCell *cell = [ThreeDPublishCollectionViewCell cellWithCollectionView:collectionView andIndexPath:indexPath andTitleArray:self.titleArray andBjImageArray:self.bjImageArray andCellId:@"ThreeDPublishSexCollectionViewCell"];
        cell.tag = 10000;
        cell.contentLabel.hidden = YES;
        cell.titleLabel.text = self.titleArray[indexPath.row];
        cell.contentImageView.contentMode = UIViewContentModeCenter;
        cell.contentImageView.image = [UIImage imageNamed:self.bjImageArray[indexPath.row]] ;
        if (self.selectIndex != indexPath.row) {
            cell.rightImage.image = [UIImage imageNamed:@"未选择"];
            cell.titleLabel.textColor = FONTCOLOR_BLACK;
        }else{
            cell.rightImage.image = [UIImage imageNamed:@"选择"];
            cell.titleLabel.textColor = UIColorFromRGB(@"#f21934", 1);
        }
        return cell;
    }else if (self.typeInt == 1){
        ThreeDPublishCollectionViewCell *cell = [ThreeDPublishCollectionViewCell cellWithCollectionView:collectionView andIndexPath:indexPath andTitleArray:self.titleArray andBjImageArray:self.bjImageArray andCellId:@"ThreeDPublishAgeCollectionViewCell"];
        cell.tag = 9999;
        cell.contentLabel.hidden = YES;
        cell.titleLabel.hidden = YES;
        cell.contentImageView.contentMode = UIViewContentModeScaleAspectFit;
        [cell.contentImageView sd_setImageWithURL:[NSURL URLWithString:self.bjImageArray[indexPath.row]]];
        if (self.selectIndex != indexPath.row) {
            cell.rightImage.image = [UIImage imageNamed:@"未选择"];
            cell.titleLabel.textColor = FONTCOLOR_BLACK;
        }else{
            cell.rightImage.image = [UIImage imageNamed:@"选择"];
            cell.titleLabel.textColor = UIColorFromRGB(@"#f21934", 1);
        }
        return cell;
    }else if (self.typeInt == 3){
        ThreeDPublishCollectionViewCell *cell = [ThreeDPublishCollectionViewCell cellWithCollectionView:collectionView andIndexPath:indexPath andTitleArray:self.titleArray andBjImageArray:self.bjImageArray andCellId:@"ThreeDPublishTypeCollectionViewCell"];
        cell.tag = 3333;
        cell.contentLabel.hidden = YES;
        cell.titleLabel.hidden = YES;
        cell.contentImageView.contentMode = UIViewContentModeScaleAspectFit;
        [cell.contentImageView sd_setImageWithURL:[NSURL URLWithString:self.bjImageArray[indexPath.row]]];
        if (self.selectIndex != indexPath.row) {
            cell.rightImage.image = [UIImage imageNamed:@"未选择"];
            cell.titleLabel.textColor = FONTCOLOR_BLACK;
        }else{
            cell.rightImage.image = [UIImage imageNamed:@"选择"];
            cell.titleLabel.textColor = UIColorFromRGB(@"#f21934", 1);
        }
        return cell;
    }else if (self.typeInt == 5){
        ThreeDPublishCollectionViewCell *cell = [ThreeDPublishCollectionViewCell cellWithCollectionView:collectionView andIndexPath:indexPath andTitleArray:self.titleArray andBjImageArray:self.bjImageArray andCellId:@"ThreeDPublishAccessoryCollectionViewCell"];
        cell.tag = 5555;
        cell.contentLabel.hidden = YES;
        cell.titleLabel.hidden = YES;
        if (self.bjImageArray.count> indexPath.row) {
            [cell.contentImageView sd_setImageWithURL:[NSURL URLWithString:self.bjImageArray[indexPath.row]]];
        }
        cell.contentImageView.contentMode = UIViewContentModeScaleAspectFit;
        if (self.selectIndex != indexPath.row) {
            cell.rightImage.image = [UIImage imageNamed:@"未选择"];
            cell.titleLabel.textColor = FONTCOLOR_BLACK;
        }else{
            cell.rightImage.image = [UIImage imageNamed:@"选择"];
            cell.titleLabel.textColor = UIColorFromRGB(@"#f21934", 1);
        }
        return cell;
    }else if (self.typeInt == 6){
        ThreeDPublishCollectionViewCell *cell = [ThreeDPublishCollectionViewCell cellWithCollectionView:collectionView andIndexPath:indexPath andTitleArray:self.titleArray andBjImageArray:self.bjImageArray andCellId:@"ThreeDPublishBjCollectionViewCell"];
        cell.tag = 6666;
        cell.contentLabel.hidden = YES;
        cell.titleLabel.text = self.titleArray[indexPath.row];
        cell.titleLabel.hidden = YES;
        cell.contentImageView.contentMode = UIViewContentModeScaleToFill;
        [cell.contentImageView sd_setImageWithURL:[NSURL URLWithString:self.bjImageArray[indexPath.row]]];
        cell.contentImageView.userInteractionEnabled = NO;
        if (self.selectIndex != indexPath.row) {
            cell.rightImage.image = [UIImage imageNamed:@"未选择"];
            cell.titleLabel.textColor = FONTCOLOR_BLACK;
        }else{
            cell.rightImage.image = [UIImage imageNamed:@"选择"];
            cell.titleLabel.textColor = UIColorFromRGB(@"#f21934", 1);
        }
        return cell;
    }else{
        ThreeDPublishCollectionViewCell *cell = [ThreeDPublishCollectionViewCell cellWithCollectionView:collectionView andIndexPath:indexPath andTitleArray:self.titleArray andBjImageArray:self.bjImageArray andCellId:@"ThreeDPublishChannelCollectionViewCell"];
        cell.tag = 7777;
        cell.contentLabel.hidden = YES;
        cell.titleLabel.text = self.titleArray[indexPath.row];
        cell.contentImageView.contentMode = UIViewContentModeScaleAspectFit;
        [cell.contentImageView sd_setImageWithURL:[NSURL URLWithString:self.bjImageArray[indexPath.row]]];
        if (self.selectIndex != indexPath.row) {
            cell.rightImage.image = [UIImage imageNamed:@"未选择"];
            cell.titleLabel.textColor = FONTCOLOR_BLACK;
        }else{
            cell.rightImage.image = [UIImage imageNamed:@"选择"];
            cell.titleLabel.textColor = UIColorFromRGB(@"#f21934", 1);
        }
        return cell;
    }
    
}


#pragma mark  点击CollectionView触发事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"-----%ld----------------",indexPath.row);
    if (self.typeInt == 2) {
        ThreeDSeasonCollectionViewCell *cell = [self viewWithTag:2222];
        cell.rightImage.image = [UIImage imageNamed:@"选择"];
        cell.titleLabel.textColor = UIColorFromRGB(@"#f21934", 1);
        self.selectIndex = indexPath.row;
        [collectionView reloadData];
        
        if (_selectClickBlock) {
            _selectClickBlock(indexPath.row);
        }
    }else if (self.typeInt == 4){
        ThreeDPublishCollectionViewCell *cell = [self viewWithTag:4444];
        cell.rightImage.image = [UIImage imageNamed:@"选择"];
        cell.titleLabel.hidden = YES;
        cell.contentLabel.hidden = NO;
        cell.contentLabel.textColor = UIColorFromRGB(@"#f21934", 1);
        self.selectIndex = indexPath.row;
        [collectionView reloadData];
        
        if (_selectClickBlock) {
            _selectClickBlock(indexPath.row);
        }
    }else if (self.typeInt == 0){
        ThreeDPublishCollectionViewCell *cell = [self viewWithTag:10000];
        cell.rightImage.image = [UIImage imageNamed:@"选择"];
        cell.titleLabel.hidden = NO;
        cell.contentLabel.hidden = YES;
        cell.contentLabel.textColor = UIColorFromRGB(@"#f21934", 1);
        self.selectIndex = indexPath.row;
        [collectionView reloadData];
        
        if (_selectClickBlock) {
            _selectClickBlock(indexPath.row);
        }
    }else if (self.typeInt == 3){
        ThreeDPublishCollectionViewCell *cell = [self viewWithTag:3333];
        cell.rightImage.image = [UIImage imageNamed:@"选择"];
        cell.titleLabel.hidden = YES;
        cell.contentLabel.hidden = YES;
        cell.contentLabel.textColor = UIColorFromRGB(@"#f21934", 1);
        self.selectIndex = indexPath.row;
        [collectionView reloadData];
        
        if (_selectClickBlock) {
            _selectClickBlock(indexPath.row);
        }
    }else if (self.typeInt == 5){
        ThreeDPublishCollectionViewCell *cell = [self viewWithTag:5555];
        cell.rightImage.image = [UIImage imageNamed:@"选择"];
        cell.titleLabel.hidden = YES;
        cell.contentLabel.hidden = YES;
        cell.contentLabel.textColor = UIColorFromRGB(@"#f21934", 1);
        self.selectIndex = indexPath.row;
        [collectionView reloadData];
        
        if (_selectClickBlock) {
            _selectClickBlock(indexPath.row);
        }
    }else if (self.typeInt == 6){
        ThreeDPublishCollectionViewCell *cell = [self viewWithTag:6666];
        cell.rightImage.image = [UIImage imageNamed:@"选择"];
        cell.titleLabel.hidden = YES;
        cell.contentLabel.hidden = YES;
        cell.contentLabel.textColor = UIColorFromRGB(@"#f21934", 1);
        self.selectIndex = indexPath.row;
        [collectionView reloadData];
        
        if (_selectClickBlock) {
            _selectClickBlock(indexPath.row);
        }
    }else if (self.typeInt == 7){
        ThreeDPublishCollectionViewCell *cell = [self viewWithTag:7777];
        cell.rightImage.image = [UIImage imageNamed:@"选择"];
        cell.titleLabel.hidden = NO;
        cell.contentLabel.hidden = YES;
        cell.contentLabel.textColor = UIColorFromRGB(@"#f21934", 1);
        self.selectIndex = indexPath.row;
        [collectionView reloadData];
        
        if (_selectClickBlock) {
            _selectClickBlock(indexPath.row);
        }
    }
    else{
        ThreeDPublishCollectionViewCell *cell = [self viewWithTag:9999];
        cell.rightImage.image = [UIImage imageNamed:@"选择"];
        cell.titleLabel.textColor = UIColorFromRGB(@"#f21934", 1);
        cell.contentLabel.hidden = YES;
        self.selectIndex = indexPath.row;
        [collectionView reloadData];
        
        if (_selectClickBlock) {
            _selectClickBlock(indexPath.row);
        }
    }
}

/*
 -(void)createSubView
 {
 NSArray *sexStrArr = @[@"男",@"女"];
 NSArray *sexPicArr = @[@"男",@"女"];
 
 for (int i = 0; i < sexStrArr.count; i ++) {
 UIView *bjView = [[UIView alloc] initWithFrame:CGRectMake(i*SCREEN_WIDTH/2, 0, SCREEN_WIDTH/2, ScreenX375(210))];
 [self addSubview:bjView];
 
 self.titleLabel = [[UILabel alloc] init];
 self.titleLabel.size = CGSizeMake(ScreenX375(14), ScreenX375(14));
 self.titleLabel.centerX = SCREEN_WIDTH/4;
 self.titleLabel.top = ScreenX375(14);
 self.titleLabel.text = sexStrArr[i];
 self.titleLabel.textColor = FONTCOLOR_BLACK;
 self.titleLabel.font = Kfont(14);
 [bjView addSubview:self.titleLabel];
 
 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
 button.frame = CGRectMake(ScreenX375(24), self.titleLabel.bottom + ScreenX375(13), bjView.width - ScreenX375(36), bjView.width - ScreenX375(36));
 button.tag = 1111+i;
 //添加边框
 CALayer * layer = [button layer];
 layer.borderColor = [RGBACOLOR(0, 0, 0, 0.1) CGColor];
 layer.borderWidth = 1.0f;
 //添加四个边阴影
 button.layer.shadowColor = RGBACOLOR(0, 0, 0, 0.1).CGColor;//阴影颜色
 button.layer.shadowOffset = CGSizeMake(1.0, 1.0);//偏移距离
 button.layer.shadowOpacity = 0.5;//不透明度
 button.layer.shadowRadius = 1.0;//半径
 [button setImage:[UIImage imageNamed:sexPicArr[i]] forState:UIControlStateNormal];
 [button addTarget:self action:@selector(selectSex:) forControlEvents:UIControlEventTouchUpInside];
 
 self.rightImage = [[UIImageView alloc] init];
 self.rightImage.frame = CGRectMake(button.width-ScreenX375(25), ScreenX375(5), ScreenX375(21), ScreenX375(21));
 self.rightImage.image = [UIImage imageNamed:@"未选择"];
 [button addSubview:self.rightImage];
 
 if (i == 1) {
 button.selected = YES;
 //            self.isSelect = YES;
 self.rightImage.image = [UIImage imageNamed:@"选择"];
 self.titleLabel.textColor = UIColorFromRGB(@"#f21934", 1);
 self.selectBtn = button;
 }
 //        else{
 //            if (i == 0) {
 //                self.isSelect = NO;
 //            }
 //        }
 [bjView addSubview:button];
 }
 }
 */

@end


